'use strict'

const { TaiSpider } = require('tai-spider');

const keywords = [
    'Anticipated Re-entries'
];

const fieldNameMap = [{
    fn: 'catNo',
    dn: 'Cat No',
    type: 'int'
}, {
    fn: 'designation',
    dn: 'Designation'
}, {
    fn: 'stName',
    dn: 'Name(SpaceTrack)'
}, {
    fn: 'predictedDate',
    dn: 'PredictedDate UTC',
    type: 'date'
}, {
    fn: 'predictionIssued',
    dn: 'PredictionIssued UTC',
    type: 'date'
}];

class AnticipatedReEntriesSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'anticipated-re-entries';
        this.debug = true;
        this.start_urls = ['http://www.zarya.info/Diaries/Launches/Decays.php'];
    }

    *parse(response) {
        for (let ele of response.css('.h3')) {
            for (let keyword of keywords) {
                if (keyword === ele.extract()) {
                    let nextEle = ele.next('center');
                    if (nextEle) {
                        let records = nextEle.parseTable({ fieldNameMap });
                        for (let record of records) {
                            yield Object.assign({
                                index_name: 'tai-anticipated',
                                action: 're-entries',
                                id: record.catNo,
                            }, record);
                        }
                    }
                }
            }
        }
    }
}

module.exports = AnticipatedReEntriesSpider;